<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px">
          <el-col :span="6">
            <el-form-item label="状态名称">
              <el-input
                v-model="listQuery.fstatename"
                clearable
                class="filter-item"
                placeholder="请输入状态名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                @click="handleFilter"
                type="primary"
                >查找</el-button
              >
              <el-button
                @click="handleCreate"
                type="success"
                >添加</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
       <!-- 查询结果 -->
    <JNPF-table
      v-loading="listLoading"
      :data="list"
    >
    <el-table-column align="center" :width="buttonColor.operationTwo" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column
        :width="buttonColor.line"
        :index="indexMethod"
        align="center"
        label="序"
        type="index"
      />
      <el-table-column align="left" label="状态编号" prop="fstateno" />
      <el-table-column align="left" label="状态名称" prop="fstatename" />
      <el-table-column align="center" label="数据采集" prop="fdataflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.fdataflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.fdataflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="巡查" prop="fcheckflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.fcheckflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.fcheckflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="保养" prop="fmaintainflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.fmaintainflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.fmaintainflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="维修" prop="frepairflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.frepairflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.frepairflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="调拨" prop="fmovflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.fmovflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.fmovflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="报废" prop="fmrkflag">
        <template slot-scope="scope">
          <i
            v-if="scope.row.fmrkflag === 1"
            style="color: #00ff60; font-size: 25px"
            class="el-icon-circle-check"
          />
          <i
            v-if="scope.row.fmrkflag === 0"
            style="color: #ff0000; font-size: 25px"
            class="el-icon-circle-close"
          />
        </template>
      </el-table-column>
    </JNPF-table>
    <paginations
      :total="total"
      :page.sync="listQuery.currentPage"
      :limit.sync="listQuery.pageSize"
      />
      </div>
    </div>
    <el-dialog
      width="80%"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :title="textMap[dialogStatus]"
    >
     <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form ref="dataForm" :rules="rules" :model="dataForm" @submit.native.prevent label-width="80px">
          <el-col :span="12">
             <el-form-item label="状态编号" prop="fstateno">
              <el-input :disabled="dialogStatus!='create'" v-model="dataForm.fstateno" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="状态名称"
              prop="fstatename"
            >
              <el-input v-model="dataForm.fstatename" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="数据采集"
              prop="fdataflag"
            >
              <el-select
                v-model="dataForm.fdataflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否数据采集"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="巡查" prop="fcheckflag">
              <el-select
                v-model="dataForm.fcheckflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否可巡查"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="保养"
              prop="fmaintainflag"
            >
              <el-select
                v-model="dataForm.fmaintainflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否可保养"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="维修" prop="frepairflag">
              <el-select
                v-model="dataForm.frepairflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否可维修"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调拨" prop="fmovflag">
              <el-select
                v-model="dataForm.fmovflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否可调拨"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报废" prop="fmrkflag">
              <el-select
                v-model="dataForm.fmrkflag"
                clearable
                filterable
                class="filter-item"
                placeholder="是否可报废"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" type="info">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData"
          >确定</el-button
        >
        <el-button v-else @click="updateData" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listequipmentState,
  createequipmentState,
  updateequipmentState,
  deleteequipmentState,
} from "@/api/equipmentState";
import Pagination from "@/components/Pagination";
import moment from "moment";
import { tableColor,titleColor,buttonColor } from "@/filters/index";
export default {
  name: "STsEquipmentState",
  components: { Pagination },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fstatename: undefined,
        fdataflag: undefined,
        fcheckflag: undefined,
        fmaintainflag: undefined,
        frepairflag: undefined,
        fmovflag: undefined,
        fmrkflag: undefined,
        fupddate: undefined,
        foperator: undefined,
        sort: "create_time",
        order: "desc",
      },
      siteTypeList: [
        { value: 1, label: "是" },
        { value: 0, label: "否" },
      ],
      dataForm: {
        fstateno: undefined,
        fstatename: undefined,
        fdataflag: undefined,
        fcheckflag: undefined,
        fmaintainflag: undefined,
        frepairflag: undefined,
        fmovflag: undefined,
        fmrkflag: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      tableColor: tableColor,
      titleColor: titleColor,
      rules: {
        fstateno: [{ required: true, message: "状态编号不能为空" }],
        fstatename: [{ required: true, message: "状态名称不能为空" }],
        fdataflag: [
          { required: true, message: "数据采集不能为空" },
          {
            type: "number",
            message: "数据采集必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fcheckflag: [
          { required: true, message: "可巡查不能为空" },
          {
            type: "number",
            message: "可巡查必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fmaintainflag: [
          { required: true, message: "可保养不能为空" },
          {
            type: "number",
            message: "可保养必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        frepairflag: [
          { required: true, message: "可维修不能为空" },
          {
            type: "number",
            message: "可维修必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fmovflag: [
          { required: true, message: "可调拨不能为空" },
          {
            type: "number",
            message: "可调拨必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fmrkflag: [
          { required: true, message: "可报废不能为空" },
          {
            type: "number",
            message: "可报废必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fupddate: [],
        foperator: [],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property];
      if (date === undefined || date === null) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    getList() {
      this.listLoading = true;
      listequipmentState(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        fstateno: undefined,
        fstatename: undefined,
        fdataflag: undefined,
        fcheckflag: undefined,
        fmaintainflag: undefined,
        frepairflag: undefined,
        fmovflag: undefined,
        fmrkflag: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined,
      };
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      this.dataForm.fdataflag=1
      this.dataForm.fcheckflag=1
      this.dataForm.fmaintainflag=1
      this.dataForm.frepairflag=1
      this.dataForm.fmovflag=1
      this.dataForm.fmrkflag=1
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator=this.$store.state.user.id
          this.dataForm.fupddate=new Date()
          createequipmentState(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加设备状态成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator=this.$store.state.user.id
          this.dataForm.fupddate=new Date()
          updateequipmentState(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fstateno === this.dataForm.fstateno) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新设备状态成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否要删除设备状态", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteequipmentState(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除设备状态成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response,
            });
          });
      });
    },
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>